﻿using Android.Content;
using CarouselView.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Application = Android.App.Application;
using Environment = Android.OS.Environment;
using Net = Android.Net;

namespace CarouselView.Platforms.Android
{
    public class DirectoryService:IDirectoryContract
    {
        /// <summary>
        /// 获取文档目录
        /// </summary>
        /// <returns></returns>
        public string GetDocumentDirectory()
        {
            //return Application.Context.GetExternalFilesDir(Environment.DirectoryPictures).AbsoluteFile.Path;
            //var dir = $"{Environment.ExternalStorageDirectory.AbsoluteFile.Path}/sifang";
            var dir = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDocuments).AbsolutePath;
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            return dir;
        }

        /// <summary>
        /// 获取图片目录
        /// </summary>
        /// <returns></returns>
        public string GetPictureDirectory()
        {
            var dir = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures).AbsolutePath;
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            return dir;
        }

        /// <summary>
        /// 广播
        /// </summary>
        public void SendBroadcast(string path)
        {
            Intent intent = new Intent(Intent.ActionMediaScannerScanFile);
            Net.Uri uri = Net.Uri.FromFile(new Java.IO.File(path));
            intent.SetData(uri);
            Application.Context.SendBroadcast(intent);
        }
    }
}
